﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DAL;

namespace PeisongWeb
{
    public partial class recoverPasswd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btn_getPwd_Click(object sender, EventArgs e)
        {
            if (Request.Cookies["CheckCode"] == null)
            {

                Library.Script.ClientMsg("你的浏览器阻止了cookie,网站需要开启cookie功能.");

                return;
            }

            if (String.Compare(Request.Cookies["CheckCode"].Value, txt_validateCode.Value, true) != 0)
            {

                Library.Script.ClientMsg("你输入的验证码不正确，请重试.");
                return;
            }
            if (!logic.customer.isExistUser(txt_username.Value))
            {
                Library.Script.ClientMsg("用户名不存在，请输入正确的用户名！");
                return;
            }
            else if (!emailIsSure(txt_username.Value.Trim(), txt_email.Value.Trim()))
            {
                Library.Script.ClientMsg("您输入的邮箱不正确，请重输！");
                return;
            }
            else
            {
                string username = txt_username.Value.Trim();
                string ranpwd = getRanPwd(6);
                updatePwd(username, ranpwd);
                try
                {
                    logic.sendEmail.sendNewpwd(username, ranpwd);
                    Library.Script.ClientMsgUrl("新密码已经发送到您绑定的邮箱，请查收！", "login.html");
                }
                catch
                {
                    DAL.SysLogEntity log = new DAL.SysLogEntity();
                    log.SlInfor = "发送密码修改邮件失败！";
                    log.SlServerity = "A";
                    log.SlEmergency = "A";
                    log.SlUrl = Request.Url.ToString();
                    log.Slip = HttpContext.Current.Request.UserHostAddress.ToString();
                    logic.sysLog.insert(log);
                    Library.Script.ClientMsgUrl("修改密码失败，请与客服人员联系！", "login.html");
                }
            }
        }

        /// <summary>
        /// 随机密码
        /// </summary>
        /// <param name="StrLength">长度</param>
        /// <returns></returns>
        public string getRanPwd(int StrLength)
        {
            Random ran = new Random();
            string str = "";
            int i = 0;
            do
            {
                int num = (int)(ran.NextDouble() * 122);
                if (num > 47)
                {
                    if (!(((num > 57) & (num < 65)) | ((num > 90) & (num < 97))))
                    {
                        str = str + Convert.ToChar(num);
                        i++;
                    }
                }
            }
            while (i < StrLength);
            return str.ToLower();
        }

        /// <summary>
        /// 修改随机生成的密码
        /// </summary>
        /// <param name="username"></param>
        /// <param name="ranpwd"></param>
        void updatePwd(string username, string ranpwd)
        {
            DataRow dr = logic.customer.getByUsername(username);
            if (dr != null)
            {
                int userid = Convert.ToInt32(dr["customerId"]);
                CustomerEntity _cus = new CustomerEntity();
                _cus.CustomerId = userid;
                _cus.Password = Library.StringLbl.EncryptMD5(ranpwd);
                logic.customer.updatePwd(_cus);
            }
        }

        /// <summary>
        /// 查出用户所绑定的邮箱是否正确
        /// </summary>
        /// <param name="username"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        private bool emailIsSure(string username, string email)
        {
            bool flag = false;
            DataRow dr = logic.customer.getByUsername(username);
            if (dr != null)
            {
                string userEmail = dr["email"].ToString();
                if (email == userEmail)
                    flag = true;
                else
                    flag = false;
            }
            return flag;
        }
    }
}
